package com.zzyl.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.constant.CacheConstant;
import com.zzyl.dto.UserDto;
import com.zzyl.service.UserService;
import com.zzyl.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理相关接口")
public class UserController {

    @Autowired
    private UserService userService;



    @PostMapping("/page/{pageNum}/{pageSize}")
    @ApiOperation("分页查询")
    public ResponseResult<PageResponse<UserVo>> getByPage(@RequestBody UserDto userDto,
                                                          @PathVariable Integer pageNum,
                                                          @PathVariable Integer pageSize ) {
        PageResponse<UserVo> page=userService.getByPage1(userDto,pageNum,pageSize);
//        PageResponse<UserVo> page=userService.getByPage(userDto,pageNum,pageSize);


        return ResponseResult.success(page);
    }

    @PutMapping
    @ApiOperation("新增用户")
    public ResponseResult add(@RequestBody UserDto userDto){
        userService.add(userDto);
        return ResponseResult.success();
    }

    @PatchMapping
    @ApiOperation("修改用户")
    public ResponseResult<UserVo> update(@RequestBody UserDto userDto){
        UserVo userVo=userService.update(userDto);
        return ResponseResult.success(userVo);
    }

    @PutMapping("/is-enable/{id}/{status}")
    @ApiOperation("启用禁用")
    public ResponseResult updateStatus(@PathVariable Long id,
                                       @PathVariable String status){
        userService.updateStatus(id,status);
        return ResponseResult.success();
    }

    @DeleteMapping("/remove/{userId}")
    @ApiOperation("删除用户")
    public ResponseResult delete(@PathVariable Long userId){

        userService.delete(userId);
        return ResponseResult.success();
    }

    @PostMapping("/reset-passwords/{userId}")
    @ApiOperation("重置密码")
    public ResponseResult resetPw(@PathVariable Long userId){

        userService.resetPw(userId);
        return ResponseResult.success();
    }

    @PostMapping("/list")
    @ApiOperation("用户列表")
    @ApiImplicitParam(name = "userDto", value = "用户DTO对象", required = true, dataType = "UserDto")
    @ApiOperationSupport(includeParameters = {"UserDto.realName"})
    public ResponseResult<UserVo> userList(@RequestBody UserDto userDto){
        UserVo userVo=userService.userList(userDto);
        return ResponseResult.success(userVo);
    }




}
